<style lang="less">
    @import './login.less';
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con" :style="{left:Left,'margin-left':MarginLeft}">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <FormItem prop="userName">
                            <Input v-model="form.userName" placeholder="请输入用户名">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input type="password" v-model="form.password" placeholder="请输入密码">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip"></p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import axios from 'axios'
import util from '../util/util'
export default {
    data () {
        return {
            Left:'auto',
            MarginLeft:'auto',
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    components:{
        
    },
    created(){
        this.Left = (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) ? '50%' : 'auto';
        this.MarginLeft = (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) ? '-150px' : 'auto';
    },
    mounted(){
        
    },
    methods: {
        handleSubmit () {
            let vm = this;
            this.$refs.loginForm.validate((valid) => {
                if(valid){
                    //vm.$router.push({name:'home'});
                    util.ajax({
                        method: 'post',
                        url: '/auth/api-token-auth/',
                        data: {
                            'username': vm.form.userName,
                            'password': vm.form.password
                        }
                    }).then(function (response) {
                        if (response.data.token) {
                            //vm.$store.commit('updateToken',response.data.token);
                            sessionStorage.setItem('api_token', response.data.token);
                            sessionStorage.setItem('Uname',vm.form.userName);
                            vm.$store.commit('UpdateUserInfo',{Uname:vm.form.userName,Upwd:vm.form.password});
                            (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) ? vm.$router.push({name:"phoneHome"}) : vm.$router.push({name:"home_index"}); 
                        } else {
                            vm.$Message.error('登陆失败，请检查账号和密码');
                        }
                    }).catch(function (error) {
                        vm.$Message.error('登录失败');
                    })
                }
            });
        }
    }
};
</script>

<style>

</style>
